package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Adspace;
import com.jspgou.cms.entity.Advertise;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.AdspaceMng;
import com.jspgou.cms.manager.AdvertiseMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.RequestUtils1;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class AdvertiseFroAct {
    /**
     * 店铺广告列表页
    */
    public static final String TPL_ADVERTISE_LIST = "tpl.advertiseList";
   /**
     * 店铺广告添加页
     */
    public static final String TPL_ADVERTISE_ADD = "tpl.advertiseAdd";
    /**
     * 店铺广告修改页
     */
    public static final String TPL_ADVERTISE_EDIT = "tpl.advertiseEdit";
	
	
    //广告列表
	@RequestMapping(value = "/advertise/list*.jspx")
	public String list(String cl,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("cl", cl);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_LIST), request);
	}
	
    //广告添加
	@RequestMapping(value = "/advertise/add.jspx")
	public String add(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		List<Adspace> adspaceList = adspaceMng.getListByStoreId(store.getId(),true);
        model.addAttribute("adspaceList", adspaceList);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_ADD), request);
	}
	
    //广告保存
	@RequestMapping(value = "/advertise/save.jspx" ,method = RequestMethod.POST)
	public String save(Advertise bean,Long adspaceId,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Map<String, String> attr = RequestUtils1.getRequestMap(request, "attr_");
		// 去除为空串的属性
		Set<String> toRemove = new HashSet<String>();
		for (Entry<String, String> entry : attr.entrySet()) {
			if (StringUtils.isBlank(entry.getValue())) {
				toRemove.add(entry.getKey());
			}
		}
		for (String key : toRemove) {
			attr.remove(key);
		}
		Store store=member.getStore();
		bean.setStore(store);
		model.addAttribute("store", store);
		manager.save(bean,adspaceId,attr);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_LIST), request);
	}
	
    //广告修改
	@RequestMapping(value = "/advertise/edit.jspx")
	public String edit(Long id,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		Advertise advertise=manager.findById(id);
		model.addAttribute("advertise", advertise);
		model.addAttribute("attr", advertise.getAttr());
		model.addAttribute("adspaceList", adspaceMng.getListByStoreId(store.getId(),true));
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ADVERTISE_EDIT), request);
	}
	
    //广告更新
	@RequestMapping(value = "/advertise/update.jspx",method = RequestMethod.POST)
	public String update(Long queryAdspaceId, Boolean queryEnabled,
			Advertise bean, Long adspaceId, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		Map<String, String> attr = RequestUtils1.getRequestMap(request, "attr_");
		// 去除为空串的属性
		Set<String> toRemove = new HashSet<String>();
		for (Entry<String, String> entry : attr.entrySet()) {
			if (StringUtils.isBlank(entry.getValue())) {
				toRemove.add(entry.getKey());
			}
		}
		for (String key : toRemove) {
			attr.remove(key);
		}
		bean = manager.update(bean, adspaceId, attr);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return list(null,1,request,model);
	}
	
	//广告排序
//	@RequestMapping("/advertise/priority.jspx")
//	public String priority(Long[] wids, Integer[] priority, Integer pageNo,
//			HttpServletRequest request, ModelMap model) {
//		manager.updatePriority(wids, priority);
//		model.addAttribute("message", "global.success");
//		return list(pageNo, request, model);
//	}
	
	//广告删除
	@RequestMapping("/advertise/delete.jspx")
	public String qqDelete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		manager.deleteByIds(ids);
//		return list(pageNo, request, model);
		return "redirect:advertise/list.jspx";
	}
	
	@Autowired
	private AdvertiseMng manager;
	@Autowired
	private AdspaceMng adspaceMng;

}
